const app = getApp();
Page({
    data: {
        baseUrl: app.globalData.baseUrl,
        fileUrl: app.globalData.fileUrl,
        currentColor: '#000000',
        isDrawing: false,
        lastX: 0,
        lastY: 0,
        signFlag: false
    },

    onReady: function() {
        this.ctx = wx.createCanvasContext('signatureCanvas')
        this.setCanvasSize()
    },
    onLoad(options){
        this.setData({
            trainId:options.trainId,
            projectType:options.projectType,
            examId:options.examId,
            sapId:options.sapId
        })
    },

    // 设置canvas尺寸
    setCanvasSize: function() {
        const systemInfo = wx.getSystemInfoSync()
        const canvasWidth = systemInfo.windowWidth * 0.9
        const canvasHeight = systemInfo.windowHeight * 0.4

        this.setData({
            canvasWidth,
            canvasHeight
        })
    },

    // 触摸开始
    handleTouchStart: function(e) {
        const touch = e.touches[0]
        this.setData({
            isDrawing: true,
            signFlag: true,
            lastX: touch.x,
            lastY: touch.y
        })
    },

    // 触摸移动
    handleTouchMove: function(e) {
        if (!this.data.isDrawing) return

        const touch = e.touches[0]
        const { lastX, lastY, currentColor } = this.data

        // 绘制路径
        this.ctx.setStrokeStyle(currentColor)
        this.ctx.setLineWidth(2)
        this.ctx.setLineCap('round')
        this.ctx.beginPath()
        this.ctx.moveTo(lastX, lastY)
        this.ctx.lineTo(touch.x, touch.y)
        this.ctx.stroke()
        this.ctx.draw(true)

        this.setData({
            lastX: touch.x,
            lastY: touch.y
        })
    },

    // 触摸结束
    handleTouchEnd: function() {
        this.setData({
            isDrawing: false
        })
    },

    // 改变笔触颜色
    changeColor: function(e) {
        const color = e.currentTarget.dataset.color
        this.setData({
            currentColor: color
        })
    },

    // 清除画布
    handleClear: function() {
        this.ctx.clearRect(0, 0, this.data.canvasWidth, this.data.canvasHeight)
        this.ctx.draw(true)
        this.setData({
            signFlag: false
        })
    },

    // 保存签名
    handleSave: function() {
        var that = this;
        if(!that.data.signFlag){
            wx.showToast({
                title: '签名不能为空',
                icon:'none'
            })
            return;
        }
        wx.canvasToTempFilePath({
            canvasId: 'signatureCanvas',
            success: (res1) => {
                // 保存到相册
                wx.uploadFile({
                    url: that.data.fileUrl + '/common/picUpload', // 上传的服务器地址
                    filePath: res1.tempFilePath, // 图片的本地地址
                    name: 'file', // 服务端用于接收文件的参数名
                    success(res2) {
                        let json = JSON.parse(res2.data);
                        let fileName = json.fileName;
                        let data={
                            sapId:that.data.sapId,
                            courseId:that.data.courseId,
                            trainId:that.data.trainId,
                            examId:that.data.examId,
                            images:fileName
                        };
                        wx.request({
                            url: that.data.baseUrl+'/staff/course/exam/sign',
                            method: 'POST',
                            data:data,
                            header: {
                                'Content-Type': 'application/json' // 请求头
                            },
                            success(res3) {
                                if(res3.data.code!=200){
                                    wx.showToast({
                                        title: res3.data.msg,
                                        icon: 'none',
                                        duration: 3000
                                    });
                                    wx.hideLoading();
                                    return;
                                }
                                wx.showModal({
                                    title: '提示',
                                    content: res3.data.msg,
                                    showCancel: false,
                                    success: function (res) {
                                        if (res.confirm) {
                                            wx.redirectTo({
                                                url: '/pages/my-error/my-error?examId=' + that.data.examId
                                            })
                                        }
                                    }
                                })
                            },fail(res3){
                                wx.hideLoading();
                                wx.showToast({
                                    title: '签名保存失败，请联系管理人员！',
                                    icon: 'none',
                                    duration: 3000
                                });
                            }
                        });
                    }
                })
            },
            fail: (err) => {
                console.error('生成图片失败', err)
            }
        })
    }
})